<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="formRef" label-width="220px" :rules="rules" :model="userInfo">
      <!--手机 -->

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="editBaseInfo">保存更新</el-button>
        <el-button @click="$router.back('')">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { queryBaseInfoById, updateBaseInfoById } from '@/api/employees'
import UploadImg from '@/components/UploadImg/UploadImg.vue'
export default {
  components: { UploadImg },
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不可为空!', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getBaseInfo()
  },
  methods: {
    // 获取员工的基本信息
    async getBaseInfo() {
      try {
        const res = await queryBaseInfoById(this.$route.query.id)
        // console.log(res)
        this.userInfo = { ...res.data }
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 修改员工信息
    editBaseInfo() {
      this.$refs.formRef.validate(async(valid) => {
        if (valid) {
          try {
            const res = await updateBaseInfoById(this.userInfo)
            // console.log(res)
            this.$message.success(res.message)
          } catch (error) {
            this.$message.error(error.message)
          }
        } else {
          this.$message.error('按照格式填写内容,亲')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
